<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="项目ID" prop="projectId">
        <el-input
          v-model="queryParams.projectId"
          placeholder="请输入项目ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="工程ID" prop="engineeringId">
        <el-input
          v-model="queryParams.engineeringId"
          placeholder="请输入工程ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="人员ID" prop="personnelEmissionId">
        <el-input
          v-model="queryParams.personnelEmissionId"
          placeholder="请输入人员排放ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="电消耗量" prop="avgElectricity">
        <el-input
          v-model="queryParams.avgElectricity"
          placeholder="请输入平均用电消耗量(kWh/年)"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="设计年限" prop="designLife">
        <el-input
          v-model="queryParams.designLife"
          placeholder="请输入工程设计年限"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="电网碳因" prop="gridCarbonFactor">
        <el-input
          v-model="queryParams.gridCarbonFactor"
          placeholder="请输入电网碳排放因子(kgCO2e/kWh)"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="种植方式" prop="plantingMethod">
        <el-input
          v-model="queryParams.plantingMethod"
          placeholder="请输入绿地种植方式"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="种植面积" prop="plantingArea">
        <el-input
          v-model="queryParams.plantingArea"
          placeholder="请输入种植面积(㎡)"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['business:operationPhase:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['business:operationPhase:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['business:operationPhase:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="info"
          plain
          icon="el-icon-upload2"
          size="mini"
          @click="upload.open = true"
          v-hasPermi="['temu:goods:import']"
        >导入</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['business:operationPhase:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList" :columns="columns"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="operationPhaseList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="表ID" align="center" prop="operationId" v-if="columns[0].visible" />
      <el-table-column label="项目ID" align="center" prop="projectId" v-if="columns[1].visible" />
      <el-table-column label="工程ID" align="center" prop="engineeringId" v-if="columns[2].visible" />
      <el-table-column label="设备信息ID集合" align="center" prop="equipmentInfoIds" v-if="columns[3].visible" />
      <el-table-column label="材料运输信息ID集合" align="center" prop="materialTransportIds" v-if="columns[4].visible" />
      <el-table-column label="人员排放ID" align="center" prop="personnelEmissionId" v-if="columns[5].visible" />
      <el-table-column label="平均用电消耗量(kWh/年)" align="center" prop="avgElectricity" v-if="columns[6].visible" />
      <el-table-column label="工程设计年限" align="center" prop="designLife" v-if="columns[7].visible" />
      <el-table-column label="电网碳排放因子(kgCO2e/kWh)" align="center" prop="gridCarbonFactor" v-if="columns[8].visible" />
      <el-table-column label="绿地种植方式" align="center" prop="plantingMethod" v-if="columns[9].visible" />
      <el-table-column label="种植面积(㎡)" align="center" prop="plantingArea" v-if="columns[10].visible" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['business:operationPhase:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['business:operationPhase:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改运营维护阶段对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="项目ID" prop="projectId">
          <el-input v-model="form.projectId" placeholder="请输入项目ID" />
        </el-form-item>
        <el-form-item label="工程ID" prop="engineeringId">
          <el-input v-model="form.engineeringId" placeholder="请输入工程ID" />
        </el-form-item>
        <el-form-item label="设备信息ID集合" prop="equipmentInfoIds">
          <el-input v-model="form.equipmentInfoIds" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="材料运输信息ID集合" prop="materialTransportIds">
          <el-input v-model="form.materialTransportIds" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="人员排放ID" prop="personnelEmissionId">
          <el-input v-model="form.personnelEmissionId" placeholder="请输入人员排放ID" />
        </el-form-item>
        <el-form-item label="平均用电消耗量(kWh/年)" prop="avgElectricity">
          <el-input v-model="form.avgElectricity" placeholder="请输入平均用电消耗量(kWh/年)" />
        </el-form-item>
        <el-form-item label="工程设计年限" prop="designLife">
          <el-input v-model="form.designLife" placeholder="请输入工程设计年限" />
        </el-form-item>
        <el-form-item label="电网碳排放因子(kgCO2e/kWh)" prop="gridCarbonFactor">
          <el-input v-model="form.gridCarbonFactor" placeholder="请输入电网碳排放因子(kgCO2e/kWh)" />
        </el-form-item>
        <el-form-item label="绿地种植方式" prop="plantingMethod">
          <el-input v-model="form.plantingMethod" placeholder="请输入绿地种植方式" />
        </el-form-item>
        <el-form-item label="种植面积(㎡)" prop="plantingArea">
          <el-input v-model="form.plantingArea" placeholder="请输入种植面积(㎡)" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 导入运营维护阶段对话框 -->
    <excel-upload
      :title="upload.title"
      :uploadUrl="upload.url"
      :open="upload.open"
      @uploadClose="upload.open = false"
      @handleFileSuccess="getList">
    </excel-upload>
  </div>
</template>

<script>
import { listOperationPhase, getOperationPhase, addOperationPhase, updateOperationPhase, delOperationPhase } from "@/api/business/operationPhase"

export default {
  name: "OperationPhase",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 运营维护阶段表格数据
      operationPhaseList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 导入Excel参数
      upload: {
        // 是否显示弹出层
        open: false,
        // 弹出层标题
        title: "运营维护阶段导入",
        // 上传的地址
        url: "/business/operationPhase/importData"
      },
      // 显隐列
      columns: [
          { key: 1, label: `表ID`, visible: true },
          { key: 2, label: `项目ID`, visible: true },
          { key: 3, label: `工程ID`, visible: true },
          { key: 4, label: `设备信息ID集合`, visible: true },
          { key: 5, label: `材料运输信息ID集合`, visible: true },
          { key: 6, label: `人员排放ID`, visible: true },
          { key: 7, label: `平均用电消耗量(kWh/年)`, visible: true },
          { key: 8, label: `工程设计年限`, visible: true },
          { key: 9, label: `电网碳排放因子(kgCO2e/kWh)`, visible: true },
          { key: 10, label: `绿地种植方式`, visible: true },
          { key: 11, label: `种植面积(㎡)`, visible: true },
      ],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        projectId: null,
        engineeringId: null,
        equipmentInfoIds: null,
        materialTransportIds: null,
        personnelEmissionId: null,
        avgElectricity: null,
        designLife: null,
        gridCarbonFactor: null,
        plantingMethod: null,
        plantingArea: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        projectId: [
          { required: true, message: "项目ID不能为空", trigger: "blur" }
        ],
        engineeringId: [
          { required: true, message: "工程ID不能为空", trigger: "blur" }
        ],
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询运营维护阶段列表 */
    getList() {
      this.loading = true
      listOperationPhase(this.queryParams).then(response => {
        this.operationPhaseList = response.rows
        this.total = Number(response.total)
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        operationId: null,
        projectId: null,
        engineeringId: null,
        equipmentInfoIds: null,
        materialTransportIds: null,
        personnelEmissionId: null,
        avgElectricity: null,
        designLife: null,
        gridCarbonFactor: null,
        plantingMethod: null,
        plantingArea: null,
        createById: null,
        createBy: null,
        createTime: null,
        updateById: null,
        updateBy: null,
        updateTime: null,
        remark: null
      }
      this.resetForm("form")
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm")
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.operationId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = "添加运营维护阶段"
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const operationId = row.operationId || this.ids
      getOperationPhase(operationId).then(response => {
        this.form = response.data
        this.open = true
        this.title = "修改运营维护阶段"
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.operationId != null) {
            updateOperationPhase(this.form.operationId, this.form).then(response => {
              this.$modal.msgSuccess("修改成功")
              this.open = false
              this.getList()
            })
          } else {
            addOperationPhase(this.form).then(response => {
              this.$modal.msgSuccess("新增成功")
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const operationIds = row.operationId || this.ids
      this.$modal.confirm('是否确认删除运营维护阶段编号为"' + operationIds + '"的数据项？').then(function() {
        return delOperationPhase(operationIds)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess("删除成功")
      }).catch(() => {})
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('business/operationPhase/export', {
        ...this.queryParams
      }, `operationPhase_export_${this.parseTime(new Date())}.xlsx`)
    }
  }
}
</script>
